﻿/****** Object:  Table [dbo].[Version]******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[Version]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [dbo].[Version](
	[VersionNumber] [varchar](10) NOT NULL,
 CONSTRAINT [PK_Version] PRIMARY KEY CLUSTERED 
(
	[VersionNumber] ASC
) ON [PRIMARY]
) ON [PRIMARY]
END 

/****** Object:  Table [dbo].[SilverlightDesktopUsers]  ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[SilverlightDesktopUsers]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [dbo].[SilverlightDesktopUsers](
	[UserID] [int] IDENTITY(1,1) NOT NULL,
	[UserName] [nvarchar](50) NOT NULL,
	[Password] [nvarchar](50) NOT NULL,
	[FirstName] [nvarchar](50) NULL,
	[LastName] [nvarchar](50) NULL,
	[Email] [nvarchar](50) NULL,
	[Role] [nvarchar](50) NOT NULL,
	[LoginFailures] [int] NOT NULL,
	[Active] [bit] NOT NULL,
	[SuperUser] [bit] NOT NULL,
	[SilverlightKey] [int] NOT NULL,
 CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED 
(
	[UserID] ASC
) ON [PRIMARY]
) ON [PRIMARY]
END

/****** Object:  Table [dbo].[SilverlightDesktopLog] ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[SilverlightDesktopLog]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [dbo].[SilverlightDesktopLog](
	[LogID] [int] IDENTITY(1,1) NOT NULL,
	[UserName] [nvarchar](50) NULL,
	[LogDescription] [nvarchar](250) NOT NULL,
	[LogDate] [datetime] NOT NULL CONSTRAINT [DF_SilverlightDesktopLog_LogDate]  DEFAULT (getdate()),
 CONSTRAINT [PK_SilverlightDesktopLog] PRIMARY KEY CLUSTERED 
(
	[LogID] ASC
) ON [PRIMARY]
) ON [PRIMARY]
END


/****** Object:  Table [dbo].[SilverlightDesktopSettings] ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[SilverlightDesktopSettings]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [dbo].[SilverlightDesktopSettings](
	[Setting] [nvarchar](50) NOT NULL,
	[SettingValue] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_SilverlightDesktopSettings] PRIMARY KEY CLUSTERED 
(
	[Setting] ASC
) ON [PRIMARY]
) ON [PRIMARY]
END

/****** Object:  Table [dbo].[SilverlightDesktopModules] ******/
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[SilverlightDesktopModules]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [dbo].[SilverlightDesktopModules](
	[ModuleID] [int] IDENTITY(1,1) NOT NULL,
	[ModuleName] [nvarchar](50) NOT NULL,
	[ModuleDescription] [nvarchar](100) NOT NULL,
	[AssemblyName] [nvarchar](50) NOT NULL,
	[ClassName] [nvarchar](50) NOT NULL,
	[IconName] [nvarchar](50) NOT NULL,
	[Role] [nvarchar](50) NOT NULL,
	[WindowSize] [int] NOT NULL,
	[AllowMultipleInstances] [bit] NOT NULL,
 CONSTRAINT [PK_SilverlightDesktopModules] PRIMARY KEY CLUSTERED 
(
	[ModuleID] ASC
) ON [PRIMARY]
) ON [PRIMARY]
END

/*** This script will only run if the version number in the databse is less than 01.00.00 ***/
if((SELECT count(*) FROM Version) = 0)
BEGIN
/*** Insert Default data ***/
DELETE FROM SilverlightDesktopSettings WHERE (Setting = N'AnonymousLogin')
INSERT INTO SilverlightDesktopSettings(Setting, SettingValue) VALUES (N'AnonymousLogin', N'True')
DELETE FROM SilverlightDesktopSettings WHERE (Setting = N'MaxLoginAttempts')
INSERT INTO SilverlightDesktopSettings(Setting, SettingValue) VALUES (N'MaxLoginAttempts', N'5')
DELETE FROM SilverlightDesktopSettings WHERE (Setting = N'SMTPServer')
INSERT INTO SilverlightDesktopSettings(Setting, SettingValue) VALUES (N'SMTPServer', N'')
DELETE FROM SilverlightDesktopSettings WHERE (Setting = N'SMTPSecure')
INSERT INTO SilverlightDesktopSettings(Setting, SettingValue) VALUES (N'SMTPSecure', N'False')
DELETE FROM SilverlightDesktopSettings WHERE (Setting = N'SMTPUserName')
INSERT INTO SilverlightDesktopSettings(Setting, SettingValue) VALUES (N'SMTPUserName', N'')
DELETE FROM SilverlightDesktopSettings WHERE (Setting = N'SMTPPassword')
INSERT INTO SilverlightDesktopSettings(Setting, SettingValue) VALUES (N'SMTPPassword', N'')
DELETE FROM SilverlightDesktopSettings WHERE (Setting = N'SMTPAuthendication')
INSERT INTO SilverlightDesktopSettings(Setting, SettingValue) VALUES (N'SMTPAuthendication', N'0')
DELETE FROM SilverlightDesktopSettings WHERE (Setting = N'SMTPFromEmail')
INSERT INTO SilverlightDesktopSettings(Setting, SettingValue) VALUES (N'SMTPFromEmail', N'')

DELETE FROM SilverlightDesktopModules WHERE (ModuleName = N'WhoAmI')
INSERT INTO SilverlightDesktopModules (ModuleName, ModuleDescription, AssemblyName, ClassName, IconName, [Role], WindowSize, AllowMultipleInstances)
VALUES(N'WhoAmI', N'Displays information about the current user', N'WhoAmI.dll', N'WhoAmI.Page', N'WhoAmI.jpg', N'Any', N'20', N'True')

	/** Update Version **/
	DELETE FROM Version
	INSERT INTO Version(VersionNumber) VALUES (N'01.00.00')
END